package com.changgou.oauth.config;

import javax.annotation.Resource;

import org.springframework.security.core.Authentication;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.oauth2.provider.ClientDetails;
import org.springframework.security.oauth2.provider.ClientDetailsService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.changgou.oauth.util.UserJwt;

/**
 * 自定义授权认证类
 * 
 * @author gaofeng
 * @date 2025/5/1 13:27
 */
@Service
public class UserDetailsServiceImpl implements UserDetailsService {
    @Resource
    ClientDetailsService clientDetailsService;

    /****
     * 自定义授权认证
     * 
     * @param username 用户名
     * @return 用户详细信息
     * @throws UsernameNotFoundException 用户名未找到异常
     */
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        // 取出身份，如果身份为空说明没有认证
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();

        // 没有认证统一采用httpBasic认证，httpBasic中存储了client_id和client_secret，开始认证client_id和client_secret
        if (authentication == null) {
            ClientDetails clientDetails = clientDetailsService.loadClientByClientId(username);
            if (clientDetails != null) {
                // 秘钥
                String clientSecret = clientDetails.getClientSecret();

                // 静态方式
                return new User(username, new BCryptPasswordEncoder().encode(clientSecret),
                    AuthorityUtils.commaSeparatedStringToAuthorityList(""));
                // 数据库查找方式
                // return new User(username,clientSecret, AuthorityUtils.commaSeparatedStringToAuthorityList(""));
            }
        }

        if (StringUtils.isEmpty(username)) {
            return null;
        }

        // 根据用户名查询用户信息
        String pwd = new BCryptPasswordEncoder().encode("szitheima");

        // 创建User对象
        String permissions = "goods_list,seckill_list";

        return new UserJwt(username, pwd, AuthorityUtils.commaSeparatedStringToAuthorityList(permissions));
    }
}
